
import $ from 'jquery';

function api(url, request){
    return new Promise((resolve, reject)=>{
        $.ajax({
            url,
            data:request,
            type:'POST',
            success:(data)=>{
                if(!data.success && data.message == "User Unauthorized"){
                    location.href=`/login`
                }
                resolve(data);
            }
        })
    });
}
api.upload = function(url,file){
    return new Promise((resolve, reject) =>{
        let dataform = new FormData();
        dataform.append('file' , file);
        $.ajax({
            url,
            data:dataform,
            type:'POST',
            contentType: false,  
            processData: false,  
            success:(data) =>{
              resolve(data)
            }
        })
    });
}

module.exports= api;
